package com.huawei;
import java.util.*;
/**
 * Created by Z-hong on 2024/7/18.
 */

public class HJ45 {
    public static class Node{
        char val;
        int count;

        public Node(char val) {
            this.val = val;
            this.count=0;
        }
        public void add(){
            count++;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count=sc.nextInt();
        String[] arr=new String[count];
        for (int i = 0; i < arr.length; i++) {
            arr[i]=sc.next();
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(getCount(arr[i]));
        }
    }

    public static int getCount(String str){
        char[] charArray = str.toCharArray();
        Node nodes[]=new Node[26];
        int num=0;
        for (int i = 0; i < charArray.length; i++) {
            char a=charArray[i];
            if(nodes[a-'a']==null){
                nodes[a-'a']=new Node(a);
                num++;
            }
            nodes[a-'a'].add();

        }
        Node arr[]=new Node[num];
        int j=0;
        for (int i = 0; i < nodes.length; i++) {
            if (nodes[i]!=null){
                arr[j++]=nodes[i];
            }
        }

        Arrays.sort(arr,(a,b)->b.count-a.count);

        int mark=26;
        int res=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==null){break;}
            res+=(mark*arr[i].count);
            mark--;
        }
        return res;
    }
}
